/**
	用户
 */
package dao

import (
	"github.com/jinzhu/gorm"
)

type User struct{
	Uid int `json:"uid" gorm:"column:uid"`
	Rid int `json:"rid" gorm:"column:rid"`
	Dpid int `json:"dp_id" gorm:"column:dp_id"`
	Nickname string `json:"nickname" gorm:"column:nickname"`
	Username string `json:"username" gorm:"column:username"`
	Desc string `json:"desc" gorm:"column:desc"`
	Password string `json:"password" gorm:"column:password"`
	Status int `json:"status" gorm:"column:status"`
	Ctime int64 `json:"c_time" gorm:"column:c_time"`
	Dtime int64 `json:"d_time" gorm:"column:d_time"`
	//FormatTime string
}

type Users struct{
	User
	RoleTitle string `json:"role_title" gorm:"column:title"`
	DpName string `json:"dp_name" gorm:"column:name"`
}
/**
 读取单条数据
 */
func (u *User) Row(w map[string]interface{},page int,pageSize int)([]*Users,int,error){

	var total int = 0
	var us []*Users
	// 获取取指page，指定pagesize的记录
	err := db.Where(w).Table("bc_user").
		Select("bc_user.*,role.title,department.name").
		Joins("left join bc_role as role on role.rid=bc_user.rid").
		Joins("left join bc_department as department on department.dp_id=bc_user.dp_id").
		Where("bc_user.d_time = ?",0).
		Limit(pageSize).Offset((page-1)*pageSize).
		Order("uid desc").Find(&us).Error
	if err != nil && err != gorm.ErrRecordNotFound{
		return nil ,0,err
	}

	// 获取总条数
	db.Model(&u).Where(w).Where("d_time=?",0).Count(&total)

	if err != nil && err != gorm.ErrRecordNotFound{
		return nil ,0,err
	}
	return us,total,nil

}
/**
	读取单条数据
 */
func (u *User) First(where map[string]interface{}) (bool,*User) {

	err := db.Where(where).First(&u).Error
	if err != nil{
		return false,nil
	}
	if u.Uid > 0{
		return true,u
	}
	return false,nil
}
/**
	更新用户
 */
func (u *User) Update(w map[string]interface{},f map[string]interface{}) bool {

	fs := &User{}

	if v,ok := f["rid"];ok{
		fs.Rid = v.(int)
	}
	if v,ok := f["dp_id"];ok{
		fs.Dpid = v.(int)
	}
	if v,ok := f["username"];ok{
		fs.Username = v.(string)
	}
	if v,ok := f["nickname"];ok{
		fs.Nickname = v.(string)
	}
	if v,ok := f["desc"];ok{
		fs.Desc = v.(string)
	}
	if v,ok := f["password"];ok{
		fs.Password = v.(string)
	}
	if v,ok := f["status"];ok{
		fs.Status = v.(int)
	}
	if v,ok := f["d_time"];ok{
		fs.Dtime = v.(int64)
	}

	err := db.Model(&u).Where(w).Updates(&fs).Error
	b := true
	if err != nil {
		b = false
	}
	return b

}
/**
	删除用户
 */
func (u *User) Delete(w map[string]interface{}) bool {

	err := db.Where(w).Delete(&u).Error
	b := true
	if err != nil{
		b = false
	}
	return b
}
/**
	填加用户
 */
func (u *User) Create(d map[string]interface{}) (bool,int) {

	var ua User
	if v,ok := d["rid"];ok{
		ua.Rid = v.(int)
	}
	if v,ok := d["dp_id"];ok{
		ua.Dpid = v.(int)
	}
	if v,ok := d["username"];ok{
		ua.Username = v.(string)
	}
	if v,ok := d["nickname"];ok{
		ua.Nickname = v.(string)
	}
	if v,ok := d["desc"];ok{
		ua.Desc = v.(string)
	}
	if v,ok := d["password"];ok{
		ua.Password = v.(string)
	}
	//if v,ok := d["format_time"];ok{
	//	ua.FormatTime = v.(string)
	//}

	ua.Status = d["status"].(int)
	ua.Ctime = d["c_time"].(int64)

	err := db.Create(&ua).Error
	b := true
	if err != nil{
		b = false
	}
	var id []int
	db.Raw("select LAST_INSERT_ID() as id").Pluck("id", &id)
	return b,id[0]
}
/**
读取单条数据及角色部门信息
*/
func (u *User) FirstRoleAndDp(where map[string]interface{}) (bool,*Users) {
	var urd Users
	err := db.Where(where).Table("bc_user").
		Select("bc_user.*,role.title,department.name").
		Joins("left join bc_role as role on role.rid=bc_user.rid").
		Joins("left join bc_department as department on department.dp_id=bc_user.dp_id").
		First(&urd).Error
	if err != nil{
		return false,nil
	}
	if urd.Uid > 0{
		return true,&urd
	}
	return false,nil
}
var UserDao = &User{}
